package cn.yyh.springcloudstu.service;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @RestController在@Controller上加了@ResponseBody,返回的是json对象
 */
@RefreshScope
@RestController
public class HelloController {

	@Value("${name}")
	private String name;
	
	private final Logger logger = Logger.getLogger(getClass());
    @Autowired
    private DiscoveryClient client;
    
    @ApiOperation(value="add方法", notes="求两个数的和")
    @ApiImplicitParams({
    	@ApiImplicitParam(paramType = "path", name = "a", value = "加数", required = true, dataType = "Integer"),
        @ApiImplicitParam(paramType = "path", name = "b", value = "被加数", required = true, dataType = "Integer")
    })
    @RequestMapping(value = "/add" ,method = RequestMethod.GET)
    public Integer add(@RequestParam Integer a, @RequestParam Integer b) {
        ServiceInstance instance = client.getLocalServiceInstance();
        Integer r = a + b;
        logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
        logger.info("name: " + name);
        return r;
    }
}